
Page({

  /**
   * 页面的初始数据
   */
  data: {
    schema:[
      {
        label:'当前显示的资源序号',
        prop:'current',
        type:'number'
      },
      {
        label:'是否显示长按菜单',
        prop:'showmenu',
        type:'picker',
        labelOptions:['是(true)','否（false）'],
        valueOptions:[true, false],
      },
      {
        label:'`origin`: 发送完整的referrer; `no-referrer`: 不发送。格式固定为 `https://servicewechat.com/{appid}/{version}/page-frame.html`，其中 {appid} 为小程序的 appid，{version} 为小程序的版本号，版本号为 0 表示为开发版、体验版以及审核版本，版本号为 devtools 表示为开发者工具，其余为正式版本；',
        prop:'referrerPolicy',
        type:'text'
      },
    ],
    formData:{
      current: 0,
      showmenu: true,
      referrerPolicy: 'no-referrer'
    },
    title: '',
    result: '',
    remark: '',
    webUrl: '',
    imageList: [],
    apiName: ''
  },
  onLoad(options) {
    console.log('options', options)
    wx.setNavigationBarTitle({
      title: options.title,
    })
    
    this.setData({
      title: options.title,
      zh:options.zh,
      remark: options.remark,
      webUrl: options.webUrl
    })
  },
  getFormData(e) {
    const formData = {}
    this.data.schema.forEach(item => {
      if(item.type === 'number') {
        if(!isNaN(+e.detail.formData[item.prop]) && e.detail.formData[item.prop] !== '') {
          formData[item.prop] = +e.detail.formData[item.prop]
        }
      } else {
        formData[item.prop] = e.detail.formData[item.prop]
      }
    })
    this.setData({
      formData: formData
    });
    console.log('formData', formData);
  },
  chooseImage() {
    const that = this;
    wx.chooseMedia({
      count: 9,
      mediaType: ['mix'],
      sizeType: ['original'],
      sourceType: ['camera', 'album'],
      camera: ['back','front'][0],
      success(res) {
        console.log(res, '====res');
        that.setData({
          imageList:res.tempFiles
        })
      }
    })
  },
  choose1(e) {
    const that = this;
    wx.previewMedia({
      sources: this.data.imageList.map(item => ({ url: item.tempFilePath, type: item.fileType, poster: item.thumbTempFilePath || '' })),
      current: this.data.formData.current,
      showmenu: this.data.formData.showmenu,
      referrerPolicy: this.data.formData.referrerPolicy,
      success(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      fail(err) {
        that.setData({
          result: err,
          apiName: e.currentTarget.dataset.apiname
        })
      }
    })
  },
  onUnload() {
  }
})